#include <stdio.h>
#include <string.h>
#include <stdlib.h>
	
typedef struct {
    int top;
    int *num;
} stack;

typedef struct {
    stack stack_in;
    stack stack_out;
} CQueue;

void stack_init(stack *s)
{
    s->top = -1;
    s->num = malloc(10000*sizeof(int));
}

void stack_destory(stack *s)
{
    s->top = -1;
    free(s->num);
    s->num = NULL;
}

int pop(stack *s)
{
    return s->num[s->top--];
}
int push(stack *s, int val)
{
    s->num[++(s->top)] = val;
    return 0;
}

CQueue* cQueueCreate() {
    CQueue *que = malloc(sizeof(CQueue));
    stack_init(&que->stack_in);
    stack_init(&que->stack_out);
    return que;
}

void cQueueAppendTail(CQueue* obj, int value) {
    push(&obj->stack_in, value);
}

int cQueueDeleteHead(CQueue* obj) {
    if(obj->stack_out.top >= 0) //B不空
		return pop(&obj->stack_out);
    else if(obj->stack_in.top < 0) //B空，A也为空
        return -1;
    else {  //B空，A不空
        int i = obj->stack_in.top;
        while(i != -1) {
            obj->stack_out.num[++(obj->stack_out.top)] = pop(&obj->stack_in);//A弹出所有元素，入栈到B, 倒序
			i--;
		}
		return pop(&obj->stack_out);
    }  
}

void cQueueFree(CQueue* obj) {
    stack_destory(&obj->stack_in);
    stack_destory(&obj->stack_out);
    free(obj);
    obj = NULL;
}


void main()
{
	int i;
	int arr[4] = {-5,12,1,2};
	CQueue* obj = cQueueCreate();
	for(i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
		cQueueAppendTail(obj, arr[i]);
	
	int param_2 = cQueueDeleteHead(obj);
	printf("%d\n",param_2);
	
	cQueueFree(obj);
}

/**
 * Your CQueue struct will be instantiated and called as such:
 * CQueue* obj = cQueueCreate();
 * cQueueAppendTail(obj, value);
 
 * int param_2 = cQueueDeleteHead(obj);
 
 * cQueueFree(obj);
*/